package com.org.manhu.chargehelper.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Environment;

/**
 * 
 * @文件名：StringUtils.java
 * @包名：com.org.manhu.tool
 * @作者：李朋辉 @QQ：892839211
 * @创建时间：2014年12月17日 下午3:54:02
 *
 * @描述：字符串操作的工具类
 */
public class StringUtils {

	/**
	 * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、 换行符组成的字符串 若输入字符串为null或空字符串，返回true
	 * 
	 * @param input
	 * @return boolean
	 */
	public static boolean isEmpty(String input) {
		if (input == null || "".equals(input))
			return true;

		for (int i = 0; i < input.length(); i++) {
			char c = input.charAt(i);
			if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * @描述：将输入的字符串中的空格、回车、制表符，换行符替换掉
	 * @param str
	 * @return
	 */
	public static String replaceBlank(String str) {
		String out = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			out = m.replaceAll("");
		}
		return out;
	}

	/**
	 * 将一个InputStream流转换成字符串
	 * 
	 * @param is
	 * @return
	 */
	public static String stream2String(InputStream is) {
		StringBuffer res = new StringBuffer();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader read = new BufferedReader(isr);
		try {
			String line;
			line = read.readLine();
			while (line != null) {
				res.append(line);
				line = read.readLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != isr) {
					isr.close();
					isr.close();
				}
				if (null != read) {
					read.close();
					read = null;
				}
				if (null != is) {
					is.close();
					is = null;
				}
			} catch (IOException e) {
			}
		}
		return res.toString();
	}

	/**
	 * @描述：将心跳打包成json
	 * @param time
	 * @return
	 */
	public static String convert2HeartJson(String time) {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("heart", time);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

	/**
	 * @描述：接受列表成功后返回的json数据
	 * @return
	 */
	public static String acceptListSuccess() {
		JSONObject jsonObject = new JSONObject();

		try {
			jsonObject.put("type", "offlineResource");
			jsonObject.put("msg", "success");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

	/**
	 * @描述：将图片后缀后面多余的字符去掉
	 * @param input
	 * @return
	 */
	public static String formatPicture(String input) {
		String out = "";
		int dotIndex = input.lastIndexOf(".");
		if (dotIndex != -1) {
			int suffixLength = input.substring(dotIndex).length();
			if (suffixLength == 4) {
				out = input;
			} else {
				out = input.substring(0, dotIndex)
						+ input.substring(dotIndex, dotIndex + 4);
			}
		}
		return out.trim();
	}

	public static String getLocalVideoPath(String id, String path) {
		String localPath = "file://mnt/sdcard/manhu/video/" + id + "/";
		return localPath + getFileName(path);
	}

	/**
	 * @描述：获取文件名
	 * @param input
	 * @return
	 */
	public static String getFileName(String input) {
		String out = "";
		int index = formatPicture(input).lastIndexOf("/");
		if (index != -1) {
			out = input.substring(index + 1).trim();
		}
		return out;
	}
	
	/**
	 * @描述：将手机端的信息打包成json
	 * @param params
	 * @return
	 */
	public static String deviceInfo2Json() {
		String out = "";
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("type", "DeviceInfo");
			JSONObject object = new JSONObject();
			object.put("IMEI", DeviceInfo.getIMEI());
			object.put("IMSI", DeviceInfo.getIMSI());
			object.put("AndroidID", DeviceInfo.getAndroidID());
			object.put("DeviceModel", DeviceInfo.getDeviceModel());
			jsonObject.put("data", object);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out = jsonObject.toString();
		return out;
	}

	/**
	 * @描述：应用安装成功后的json
	 * @param appId
	 * @return
	 */
	public static String installSuccess(String appId) {
		String out = "";
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("type", "installSuccess");
			jsonObject.put("AppID", appId);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out = jsonObject.toString();
		return out;
	}

	/**
	 * @描述：将安装应用的信息打成json
	 * @param appid
	 * @return
	 */
	public static String installJson(String appid) {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("type", "install");
			jsonObject.put("AppID", appid);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

	/**
	 * @描述：将电量信息格式化
	 * @param level
	 * @return
	 */
	public static String chargeJson(String level) {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("type", "charge");
			jsonObject.put("charge", level);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

}
